package com.huazi.www.user.wwwChaCtLuo.dao.impl;

import com.huazi.www.user.wwwChaCtLuo.dao.ChaCtDao;
import com.huazi.www.user.wwwChaCtLuo.entity.CtInfo;
import com.huazi.www.user.wwwChaCtLuo.util.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 罗义芳
 * @version 1.0
 * @date 2021/5/15 15:23
 **/
public class ChaCtDaoImpl extends BaseDao implements ChaCtDao {
    /**
     * 查询总记录条数
     *
     * @param ctId 诊断id
     * @param name 患者名字
     * @return
     */
    @Override
    public Integer totalCount(Integer ctId, String name) {
        StringBuffer sql = new StringBuffer();//new StringBuffer
        sql.append("SELECT count(1)  FROM department_ct ct,frontdesk_card ca WHERE ct.CdiagnoseNO= ca.id AND 1=1 ");//根据CdiagnoseNO查询
        ResultSet rs = null;
        int count = 0;//保存记录条数
        if ((ctId != -1 && !ctId.equals("")) && name.equals("")) {
            sql.append(" AND ct.id LIKE CONCAT('%',?,'%')");//根据id模糊查询
            rs = excuteQuery(sql.toString(), ctId);//给excuteQuery赋值
        } else if ((name != null && !name.equals("")) && ctId == -1) {
            sql.append(" AND ca.`name` like CONCAT('%',?,'%')");
            rs = excuteQuery(sql.toString(), name);
        } else if ((ctId != -1 && !ctId.equals("")) && (name != null && !name.equals(""))) {
            sql.append(" AND ct.id LIKE CONCAT('%',?,'%') AND ca.`name` like CONCAT('%',?,'%')");
            rs = excuteQuery(sql.toString(), ctId, name);
        } else {
            rs = excuteQuery(sql.toString());
        }
        try {
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll(conn, psmt, rs);
        }
        return count;
    }

    /**
     * 查询分页后的集合对象
     *
     * @param ctId     诊断id
     * @param name     患者名字
     * @param start    起始下标
     * @param pageSize 查询当前总条数
     * @return
     */
    @Override
    public List<CtInfo> findByPage(Integer ctId, String name, Integer start, Integer pageSize) {
        StringBuffer sql = new StringBuffer("SELECT ct.id,ca.`name`,ct.CTime,ct.inspectionItem,ct.diagnose,info.doctorname " +
                "FROM department_ct ct INNER JOIN frontdesk_card ca ON ct.CdiagnoseNO= ca.id INNER JOIN doctor_information info ON ct.doctorid = info.doctorId " +
                "WHERE 1=1");
        ResultSet rs = null;//查询CT信息
        //如果id不为空，那么按照id查询
        if ((ctId != null && ctId != -1) && (name == null || name.equals(""))) {
            sql.append(" AND ct.id LIKE CONCAT('%',?,'%') ORDER BY ct.CTime DESC LIMIT ?,?");
            rs = excuteQuery(sql.toString(), ctId, start, pageSize);
            //如果name不为空，那么按照name查询
        } else if ((name != null && !name.equals("")) && ctId == -1) {
            sql.append(" AND ca.`name` like CONCAT('%',?,'%') ORDER BY ct.CTime DESC LIMIT ?,?");
            rs = excuteQuery(sql.toString(), name, start, pageSize);
            //如果id和name都不为空，那么查询两个条件
        } else if ((ctId != null && ctId != -1) && (name != null && !name.equals(""))) {
            sql.append(" AND ct.id LIKE CONCAT('%',?,'%') AND ca.`name` like CONCAT('%',?,'%') ORDER BY ct.CTime DESC LIMIT ?,?");
            rs = excuteQuery(sql.toString(), ctId, name, start, pageSize);
            //否则查询全部
        } else {
            sql.append(" ORDER BY ct.CTime DESC LIMIT ?,?");
            rs = excuteQuery(sql.toString(), start, pageSize);
        }
        List<CtInfo> list = new ArrayList<>();//保存分页的集合记录
        try {
            while (rs.next()) {
                CtInfo ctInfo = new CtInfo(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6));
                list.add(ctInfo);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll(conn, psmt, rs);
        }
        return list;
    }
}
